/*
 * j4Win
 *  http://code.google.com/p/j4win/
 *
 * Copyright (C) 2006-2007 Felix Geyer, Moritz Laschinger
 *
 * This file is part of j4Win.
 *
 * j4Win is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * j4Win is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package j4win;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class Playground {
	private ImageObserver observer;
	
	private BufferedImage i;
	private Graphics g;
	private Image cell;
	
	private BufferedImage[] cells = null;
	
	public static final int FIELDWIDTH = 50;
	public static final int FIELDHEIGHT = 50;
	private Field field = null;
	
	public Playground(Toolkit toolkit, ImageObserver observer) {
		this.observer = observer;
		
		i = new BufferedImage(
			FIELDWIDTH*Field.WIDTH,
			FIELDHEIGHT*Field.HEIGHT,
			BufferedImage.TYPE_INT_ARGB
		);
		g = i.getGraphics();
		cell = toolkit.getImage( getClass().getClassLoader().getResource("gfx/cell.png") );
		ImageTracker.addImage(cell);
		
		genCells();
	}
	
	public BufferedImage drawImage(int winner) {
		for (int x = 0; (x < Field.WIDTH); x++) {
			for (int y = 0; y < Field.HEIGHT; y++) {
				g.setColor(getColor(x, y));
				g.fillRect((x * FIELDWIDTH), (y * FIELDHEIGHT), FIELDWIDTH, FIELDHEIGHT);
				g.drawImage(cells[y], (x * FIELDWIDTH), (y * FIELDHEIGHT), observer);
				
				// Draw Winner
				g.setColor(new Color(200, 200, 200));
				g.setFont(new Font("Verdana", Font.BOLD, 18));
				if (winner==3) {
					String s = Messages.getString("Scoreboard.tie");
					FontMetrics fm = g.getFontMetrics();
					g.drawString(s, FIELDWIDTH*Field.WIDTH/2-fm.stringWidth(s)/2, FIELDHEIGHT*Field.HEIGHT/3);
				}
				else if (winner == 1 || winner == 2) {
					String s = Messages.getString("Scoreboard.player") + " " + winner + Messages.getString("Scoreboard.won");
					FontMetrics fm = g.getFontMetrics();
					g.drawString(s, FIELDWIDTH*Field.WIDTH/2-fm.stringWidth(s)/2, FIELDHEIGHT*Field.HEIGHT/3);
				}
			}
		}
		
		return i;
	}
	
	public void genCells() {
		cells = new BufferedImage[Field.HEIGHT];
		
		for (int y = 0; y < Field.HEIGHT; y++) {
			BufferedImage buffImage = new BufferedImage(
					FIELDWIDTH, 
					FIELDHEIGHT, 
					BufferedImage.TYPE_INT_ARGB
			);

			Graphics buffg = buffImage.getGraphics();
			buffg.drawImage(cell, 0, 0, observer);

			Color end = new Color(38, 30, 141);
			Color start = new Color(121, 112, 241);
			int steps = Field.HEIGHT * FIELDHEIGHT;
			for (int ih = 0; ih < buffImage.getHeight(); ih++) {
				int con = (y*FIELDHEIGHT)+ih;
				int red = ((end.getRed() - start.getRed())*con / steps)+start.getRed();
				int green = ((end.getGreen() - start.getGreen())*con / steps)+start.getGreen();
				int blue = ((end.getBlue() - start.getBlue())*con / steps)+start.getBlue();
				Color cur = new Color( red, green, blue);

				for (int iw = 0; iw < buffImage.getWidth(); iw++)
					if (buffImage.getRGB(iw, ih) == -16758125) {
						buffImage.setRGB(iw, ih, cur.getRGB());
					}
			}
		cells[y] = buffImage;
		}
	}
	
	private Color getColor(int x, int y) {
		if (field==null) return Color.WHITE;
		
		switch (field.get(x, Field.HEIGHT-(y+1))) {
			case 0: return Color.WHITE;
			case 1: return Color.RED;
			case 2: return Color.GREEN;
		}
		
		throw new Error("Field value undefined");
	}
	
	public void setField(Field field, int col) {
		this.field = field;
	}
}
